import { useUserStore } from '@/store/modules/user';
import type { Banner, Food, Merchant, Promotion } from '../types';

export function useNavigation() {
  const userStore = useUserStore();

  // 处理选择商家（从搜索框）
  const handleSelectMerchant = (merchant: any) => {
    uni.navigateTo({
      url: `/pages/merchant/merchant?id=${merchant.id}`
    });
  };

  // 处理选择商品（从搜索框）
  const handleSelectProduct = (product: any) => {
    uni.navigateTo({
      url: `/pages/merchant/merchant?id=${product.merchantId}&productId=${product.id}`
    });
  };

  // 处理轮播图点击
  const handleBannerClick = (banner: Banner) => {
    if (banner.type === 'merchant') {
      goToMerchant(banner.targetId);
    } else if (banner.type === 'food' && banner.productId) {
      goToProduct(banner.targetId, banner.productId);
    }
  };

  // 跳转到商家页面
  const goToMerchant = (merchantId: number) => {
    uni.navigateTo({
      url: `/pages/merchant/merchant?id=${merchantId}`
    });
  };

  // 跳转到商家页面并定位到商品
  const goToProduct = (merchantId: number, productId: number) => {
    uni.navigateTo({
      url: `/pages/merchant/merchant?id=${merchantId}&productId=${productId}`
    });
  };

  // 跳转到美食详情
  const goToFoodDetail = (food: Food) => {
    goToProduct(food.merchantId, food.productId);
  };

  // 跳转到商家详情
  const goToMerchantDetail = (merchant: Merchant) => {
    goToMerchant(merchant.id);
  };

  // 跳转到愿望榜单
  const goToWishList = () => {
    if (!userStore.hasLogin) {
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }
    
    uni.navigateTo({
      url: '/pages/my/wishList/wishList'
    });
  };

  // 跳转到活动详情
  const goToPromotion = (promo: Promotion) => {
    goToMerchant(promo.merchantId);
  };

  // 跳转到更多页面
  const goToMore = (type: string) => {
    uni.showToast({
      title: `查看更多${type}`,
      icon: 'none'
    });
  };

  return {
    handleSelectMerchant,
    handleSelectProduct,
    handleBannerClick,
    goToMerchant,
    goToProduct,
    goToFoodDetail,
    goToMerchantDetail,
    goToWishList,
    goToPromotion,
    goToMore
  };
}